/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pkw.client;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.SecureRandom;
import pl.pkw.common.Card;
import pl.pkw.common.Tool;
import pl.pkw.common.Vote;
import pl.pkw.common.VotingData;

/**
 *
 * @author Agnieszka
 */
public class ClientTool extends Tool
{
    private final BigInteger n;
    private final BigInteger e;
    
    private static final Integer NUMBER_OF_CARD = 5;
    private static final Integer NUMBER_OF_POSITION = 3;
    
    private VotingData votingData;
    private Card templateCard;    

    private SecureRandom random = new SecureRandom();
    
    
    /**
     * 
     * @param e - server public key exponent
     * @param n - server public key modulos
     */
    public ClientTool(BigInteger e, BigInteger n) throws UnsupportedEncodingException
    {
        this.e = e;
        this.n = n;
        votingData = new VotingData();
        templateCard = new Card();
        templateCard.generate(NUMBER_OF_POSITION);
        votingData.generate(random, templateCard, NUMBER_OF_CARD);
    }

    public void blind() throws UnsupportedEncodingException
    {
        for(Card c: votingData.getCards())
        {
            c.setSecretR(Tool.r(n));
            for(Vote v: c.getVotes())
            {
                //System.out.println("blind/unb");
                v.setBlindVote(Tool.blind(v.getHashVote(), e, n, c.getSecretR()));
                //Tool.unblind(v.getHashVote(), n, c.getSecretR());
                //System.out.println("ind/unb");
                //Tool.verify(Tool.unblind(v.getHashVote(), n, c.getSecretR()), v.calculateHashVoteV(), e, n);
            }
        }
    }
    
    /**
     * return 
     * @param cardNumber 
     */
    public void blindN1(Integer cardNumber)
    {
        Card card = null;
        for(Card c: votingData.getCards())
        {
          
            System.out.println(c.getNumber() + " " + cardNumber);
                    
            if(c.getNumber().equals(cardNumber))
            {
                card = c;
            }
            else
            {
                c.setR(c.getSecretR());
            }
        }       
        
        //System.out.println(cardNumber + " " + card + " before remove " + votingData.getCards().size());
        //System.out.println(cardNumber + " " + card + " before remove " + votingData.getCards().size() + " " + card.getNumber());
        votingData.setSkipCard(card);
        votingData.getCards().remove(card);
        System.out.println("after remove " + votingData.getCards().size());
    }
    
    public void unblindSignCard() throws UnsupportedEncodingException
    {
        Card c = votingData.getSkipCard();
        for(Vote v: c.getVotes())
        {
            v.setSignUnblindVote(Tool.unblind(v.getBlindSignVote(), n, c.getSecretR()));
        }
    }
    
    public Vote vote() {
        Card c = votingData.getSkipCard();
        return c.getVotes().get(2);
    }
    
    
    public VotingData getVotingData() {
        return votingData;
    }

    
    
    public BigInteger getE() {
        return e;
    }



    public BigInteger getN() {
        return n;
    }

 

    
    
 
}
